package com.example.front.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.GatewayCreateDto;
import com.example.front.dto.GatewayDto;
import com.example.front.dto.GatewaySearchDto;
import com.example.front.serivce.GatewayService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.Task;
import com.example.passport.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


@Api(tags = "网关管理")
@RestController
@CrossOrigin
@RequestMapping("/front/gateway")
public class GatewayController {

    @Resource
    public GatewayService gatewayService;
//    @Resource
//    public TaskService taskService;


    @ApiOperation("添加网关")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid GatewayCreateDto gatewayCreateDto) {
        gatewayService.create(gatewayCreateDto);
        return new JsonResult<>();
    }


    @ApiOperation("列出所有网关")
    @GetMapping("/list")
    public JsonResult<ListDto<GatewayDto>> listRole(GatewaySearchDto condition, Pagination pagination) {
//        if (!taskService.can(UserContext.getUser().getId(), Task.DEVICE)) {
//            throw new BusinessException("无权操作");
//        }
        condition.setTenantId(UserContext.getUser().getTenantId());
        ListDto<GatewayDto> roleDtoList = gatewayService.listGateway(condition, pagination);

        return new JsonResult<>(roleDtoList);
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid GatewayDto gatewayDto) {
//        if (!taskService.can(UserContext.getUser().getId(), Task.DEVICE)) {
//            throw new BusinessException("无权操作");
//        }

        gatewayService.update(gatewayDto);
        return new JsonResult<>();
    }

    @ApiOperation("删除网关")
    @PostMapping("/delete")
    public JsonResult<String> delete(long id) {
//        if (!taskService.can(UserContext.getUser().getId(), Task.DEVICE)) {
//            throw new BusinessException("无权操作");
//        }
        gatewayService.deleteById(id, UserContext.getUser().getTenantId());
        return new JsonResult<>();
    }
}
